<template>
  <el-dialog
    v-model="dialogFormVisible"
    :close-on-click-modal="false"
    :title="title"
    width="520px"
    @close="close"
  >
    <el-form ref="formRef" label-width="100px" :model="form" :rules="rules">
      <el-form-item v-if="category" label="分配分类：" prop="category_id">
        <el-select
          v-model="form.category_id"
          placeholder="请选择需要分配成员的分类"
          style="flex-grow: 1"
        >
          <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.category_name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-else label="分配运营：" prop="admin_id">
        <el-select
          v-model="form.admin_id"
          placeholder="请选择需要分配成员的运营"
          style="flex-grow: 1"
        >
          <el-option
            v-for="item in manageList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { assignAdmin, assignCategory } from '@/api/enterpriseWeChat/department'
  import { getManageAll } from '@/api/table'
  import { getCategoryAll } from '@/api/enterpriseWeChat/category'
  export default defineComponent({
    name: 'TableEditUserAssign',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        formRef: null,
        treeRef: null,
        form: {
          corp_id: null,
          user_id: null,
          admin_id: null,
          category_id: null,
        },
        rules: {
          admin_id: [{ required: true, trigger: 'blur', message: '请选择' }],
          category_id: [{ required: true, trigger: 'blur', message: '请选择' }],
        },
        title: '',
        manageList: [],
        categoryList: [],
        category: false,
        dialogFormVisible: false,
      })

      const showEdit = async (corp_id, userid, row) => {
        state.form.corp_id = corp_id
        state.form.user_id = userid
        if (!row) {
          state.title = '分配运营'
          state.category = false
        } else {
          state.title = '分配分类'
          state.category = true
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            console.log(state.form)
            if (state.category) {
              const { message } = await assignCategory(state.form)
              $baseMessage(message, 'success', 'vab-hey-message-success')
            } else {
              const { message } = await assignAdmin(state.form)
              $baseMessage(message, 'success', 'vab-hey-message-success')
            }
            emit('fetch-data')
            close()
          }
        })
      }
      onMounted(async () => {
        const manageAll = await getManageAll()
        state.manageList = manageAll.data
        const categoryAll = await getCategoryAll()
        state.categoryList = categoryAll.data
      })
      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>

<style lang="scss" scoped></style>
